<template>
  <confirm-bar :visible="transferBarVisible">
    <template #content>
      已选&nbsp;{{ selectedRowKeys.length }}&nbsp;项
    </template>
    <template #handle>
      <a-button type="link" @click="onCancel">取消</a-button>
      <transfer-drawer
        title="复制文件"
        :confirm="confirmCopy"
        cacheKey="transfer-copy-drawer"
      >
        <a-button type="link" :disabled="selectedRowKeys.length === 0">
          复制文件
        </a-button>
      </transfer-drawer>
      <transfer-drawer
        title="转移文件"
        :confirm="confirmCut"
        cacheKey="transfer-cut-drawer"
      >
        <a-button type="link" :disabled="selectedRowKeys.length === 0">
          转移文件
        </a-button>
      </transfer-drawer>
    </template>
  </confirm-bar>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import ConfirmBar from '@/components/confirm-bar/confirm-bar.vue'
import TransferDrawer from './transfer-bar-drawer.vue'
import {
  useRowSelectionActive,
  useRowSelection,
  useLockForInUse,
  useTransferBarVisible,
  useDataSourceManager,
  useTreeSelectedInfo,
} from './provide-context.js'
import { fileMove, fileCopy, flushDataStatus } from '@/api/db.js'
import { useDbType } from './provide-context.js'
let { update } = useDataSourceManager()

import mockAPI from '@/api/simple-mock.js'
/**
 * 全局
 */
let rowSelection = useRowSelectionActive()
let { selectedRowKeys } = useRowSelection()
let lockForInUse = useLockForInUse()
let transferBarVisible = useTransferBarVisible()
let treeSelectedInfo = useTreeSelectedInfo()

/**
 * 操作逻辑
 */
function onCancel() {
  transferBarVisible.value = false
  rowSelection.value = false
  lockForInUse.value = null
  delete rowSelection.getCheckboxProps
}

if (treeSelectedInfo) {
  watch(treeSelectedInfo, onCancel)
}

/**
 * 接口
 */
let dbType = useDbType()
async function confirmCopy(state, presetsValue) {
  let timer = setTimeout(() => {
    update()
  }, 1000)
  try {
    for (const id of selectedRowKeys.value) {
      await fileCopy(
        {
          id,
          targetFolderId: [...state.path].pop(),
          strategy: presetsValue,
        },
        dbType,
      )
      await flushDataStatus({ id })
      await update()
    }

    transferBarVisible.value = false
    rowSelection.value = false
    lockForInUse.value = null
    delete rowSelection.getCheckboxProps
  } catch (e) {
    console.log(e)
  }
  clearTimeout(timer)
  update()
}
async function confirmCut(state, presetsValue) {
  let timer = setTimeout(() => {
    update()
  }, 1000)
  try {
    for (const id of selectedRowKeys.value) {
      await fileMove(
        {
          id,
          targetFolderId: [...state.path].pop(),
          strategy: presetsValue,
        },
        dbType,
      )
      await flushDataStatus({ id })
      await update()
    }
    transferBarVisible.value = false
    rowSelection.value = false
    lockForInUse.value = null
    delete rowSelection.getCheckboxProps
    await update()
  } catch (e) {
    console.log(e)
  }
  clearTimeout(timer)
  update()
}
</script>
